<?php
session_start();
include("config.php");

// 安全处理图书ID
$bookId = isset($_GET['id']) ? intval($_GET['id']) : 0;
if (!$bookId) {
    header("HTTP/1.0 404 Not Found");
    include('404.php');
    exit();
}

// 查询图书信息
$sql = "SELECT * FROM book_list WHERE id = $bookId";
$result = mysqli_query($connect, $sql);
$book = mysqli_fetch_assoc($result);

// 处理图书不存在的情况
if (!$book) {
    header("HTTP/1.0 404 Not Found");
    include('404.php');
    exit();
}

// 处理评论提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 使用session中的用户名（如果已登录）
    $username = isset($_SESSION['username']) ? $_SESSION['username'] : '访客';
    
    $commentContent = mysqli_real_escape_string($connect, $_POST['content']);
    $insertSql = "INSERT INTO comment (book_id, username, content, time) 
                  VALUES ('$bookId', '$username', '$commentContent', NOW())";
    
    if (!mysqli_query($connect, $insertSql)) {
        die("评论发布失败：" . mysqli_error($connect));
    }
    // 重定向避免重复提交
    header("Location: {$_SERVER['PHP_SELF']}?id={$bookId}");
    exit();
}

// 查询评论总数
$comCountSql = "SELECT COUNT(*) as total FROM comment WHERE book_id = $bookId";
$comCountResult = mysqli_query($connect, $comCountSql);
$commentCount = mysqli_fetch_assoc($comCountResult);
$totalComments = $commentCount['total'];

// 关闭数据库连接
mysqli_close($connect);

// 设置默认图片URL
$defaultImage = "https://picsum.photos/400/600?random=book";
$bookImg = !empty($book['img']) ? $book['img'] : $defaultImage;
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($book['book_name']) ?> - 图书详情</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f6f8;
            color: #333;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .book-header {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-bottom: 40px;
        }
        
        .book-cover {
            flex: 0 0 250px;
            height: 350px;
            overflow: hidden;
            border-radius: 8px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }
        
        .book-cover img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.3s ease;
        }
        
        .book-cover img:hover {
            transform: scale(1.03);
        }
        
        .book-info {
            flex: 1;
            min-width: 300px;
        }
        
        .book-title {
            font-size: 28px;
            color: #222;
            margin-bottom: 15px;
        }
        
        .book-meta {
            margin-bottom: 20px;
        }
        
        .book-meta p {
            margin-bottom: 10px;
            font-size: 16px;
        }
        
        .book-brief {
            background-color: #f9f9f9;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
        }
        
        .book-brief h3 {
            font-size: 18px;
            margin-bottom: 10px;
        }
        
        .book-brief p {
            font-size: 14px;
            line-height: 1.8;
        }
        
        .action-buttons {
            display: flex;
            gap: 15px;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }
        
        .btn-primary {
            background-color: #4CAF50;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #45a049;
        }
        
        .btn-secondary {
            background-color: #f44336;
            color: white;
        }
        
        .btn-secondary:hover {
            background-color: #d32f2f;
        }
        
        .section {
            margin-bottom: 30px;
        }
        
        .section-title {
            font-size: 22px;
            color: #222;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        
        .comments {
            background-color: #f9f9f9;
            padding: 20px;
            border-radius: 6px;
        }
        
        .comment-form {
            margin-bottom: 25px;
        }
        
        .comment-form textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 15px;
            resize: vertical;
            min-height: 100px;
        }
        
        .comment-form input[type="submit"] {
            background-color: #337ab7;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .comment-form input[type="submit"]:hover {
            background-color: #286090;
        }
        
        .comment-list {
            margin-top: 20px;
        }
        
        .comment-item {
            background-color: white;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 15px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
        }
        
        .comment-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
            color: #666;
            font-size: 14px;
        }
        
        .comment-content {
            font-size: 16px;
            line-height: 1.6;
        }
        
        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #337ab7;
            text-decoration: none;
        }
        
        .back-link:hover {
            text-decoration: underline;
        }
        
        @media (max-width: 768px) {
            .book-header {
                flex-direction: column;
                align-items: center;
            }
            
            .book-info {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="book_display.php" class="back-link">← 返回图书列表</a>

        <div class="book-header">
            <div class="book-cover">
                <img src="<?= $bookImg ?>" 
                     alt="<?= htmlspecialchars($book['book_name']) ?>"
                     onerror="this.src='<?= $defaultImage ?>'; this.onerror=null;">
            </div>

            <div class="book-info">
                <h1 class="book-title"><?= htmlspecialchars($book['book_name']) ?></h1>
                
                <div class="book-meta">
                    <p><strong>作者：</strong><?= htmlspecialchars($book['author']) ?></p>
                    <p><strong>出版时间：</strong><?= $book['publish_time'] ?: '未知' ?></p>
                    <p><strong>出版社：</strong><?= $book['publisher'] ?: '未知' ?></p>
                </div>

                <div class="book-brief">
                    <h3>内容简介</h3>
                    <p><?= htmlspecialchars($book['brief'] ?: '暂无简介') ?></p>
                </div>
        </div>

        <div class="section comments">
            <h2 class="section-title">评论 (<?= $totalComments ?>)</h2>

            <div class="comment-form">
                <?php if (isset($_SESSION['username'])): ?>
                <form method="post">
                    <textarea name="content" placeholder="请输入评论内容..." required></textarea>
                    <input type="submit" value="发布评论" class="btn btn-primary">
                </form>
                <?php else: ?>
                <div class="login-prompt">
                    <p>请 <a href="login.php">登录</a> 后发表评论</p>
                </div>
                <?php endif; ?>
            </div>

            <div class="comment-list">
                <?php
                include("config.php");
                $sqlCom = "SELECT * FROM comment WHERE book_id = $bookId ORDER BY time DESC";
                $comResult = mysqli_query($connect, $sqlCom);
                
                if (mysqli_num_rows($comResult) > 0) {
                    while ($comment = mysqli_fetch_assoc($comResult)) {
                ?>
                <div class="comment-item">
                    <div class="comment-header">
                        <span>评论者：<?= htmlspecialchars($comment['username']) ?></span>
                        <span>时间：<?= date('Y-m-d H:i', strtotime($comment['time'])) ?></span>
                    </div>
                    <div class="comment-content">
                        <?= htmlspecialchars($comment['content']) ?>
                    </div>
                </div>
                <?php
                    }
                } else {
                    echo '<p class="no-comments">暂无评论，快来发表你的看法吧！</p>';
                }
                mysqli_close($connect);
                ?>
            </div>
        </div>
    </div>
</body>
</html>    